﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class TE_Question_Categories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1501) == true)
                {
                    if (!IsPostBack)
                        gvQues_CatBind();
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }

        private void gvQues_CatBind()
        {
            gvQues_Cat.DataSource = School.Teaching.Teaching.Get_Question_Categories();
            gvQues_Cat.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool re = School.Teaching.Teaching.Add_Ques_Category(txtname.Text);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تمت الاضافة بنجاح";
                txtname.Text = "";
                gvQues_CatBind();

            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvQues_Cat_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }

        protected void gvQues_Cat_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvQues_Cat.EditIndex = e.NewEditIndex;
            gvQues_CatBind();
        }

        protected void gvQues_Cat_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvQues_Cat.EditIndex = -1;
            gvQues_CatBind();
        }

        protected void gvQues_Cat_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvQues_Cat.PageIndex = e.NewPageIndex;
            gvQues_CatBind();
        }

        protected void gvQues_Cat_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvQues_Cat.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteQues_Catego(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvQues_CatBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvQues_Cat_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvQues_Cat.DataKeys[e.RowIndex].Value.ToString());
            TextBox txtNmae_E = (TextBox)gvQues_Cat.Rows[e.RowIndex].FindControl("txtCat");
            bool re = School.Teaching.Teaching.UpdateQues_Catego(Id, txtNmae_E.Text);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvQues_Cat.EditIndex = -1;
                gvQues_CatBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }
    }
}